package com.pengtu.web.contract;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.commons.lang.xwork.StringUtils;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;

import com.pengtu.entity.contract.According;
import com.pengtu.entity.contract.Document;
import com.pengtu.entity.contract.Inventor;
import com.pengtu.entity.contract.Itemlist;
import com.pengtu.model.Acc;
import com.pengtu.service.contract.AccordingManager;
import com.pengtu.utils.ConvertNumber;
import com.pengtu.utils.web.Struts2Utils;
import com.pengtu.web.BaseActionSupport;

@Results({
	@Result(name="inventotSave",location="${ctx}/contract/inventor.action?cid=${cid}&decorator=${decorator}&confirm=${confirm}",type="redirect"),
	@Result(name="load",location="${ctx}/contract/inventor!view.action?cid=${cid}&decorator=${decorator}&confirm=${confirm}",type="redirect")
})

public class InventorAction extends BaseActionSupport<Inventor> {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private Inventor inventor;
	private String id;
	private AccordingManager accordingManager;
	private List<Inventor> inventorList=null;
	private String [] itemArray={};
	private String cid;
	private List<Acc> acc = new ArrayList<Acc>();
	private String decorator;
	private String confirm;

	/**
	 * decorator
	 *
	 * @return  the decorator
	 * @since   1.0.0
	 */
	
	public String getDecorator() {
		return decorator;
	}


	/**
	 * @param decorator the decorator to set
	 */
	public void setDecorator(String decorator) {
		this.decorator = decorator;
	}


	/**
	 * confirm
	 *
	 * @return  the confirm
	 * @since   1.0.0
	 */
	
	public String getConfirm() {
		return confirm;
	}


	/**
	 * @param confirm the confirm to set
	 */
	public void setConfirm(String confirm) {
		this.confirm = confirm;
	}


	public List<Acc> getAcc() {
		return acc;
	}


	public void setAcc(List<Acc> acc) {
		this.acc = acc;
	}
	

	public List<Inventor> getInventorList() {
		return inventorList;
	}

	public void setInventorList(List<Inventor> inventorList) {
		this.inventorList = inventorList;
	}

	public String getCid() {
		return cid;
	}

	public void setCid(String cid) {
		this.cid = cid;
	}

	public String[] getItemArray() {
		return itemArray;
	}

	public void setItemArray(String[] itemArray) {
		this.itemArray = itemArray;
	}

	public Inventor getInventor() {
		return inventor;
	}

	public void setInventor(Inventor inventor) {
		this.inventor = inventor;
	}

	public String getId() {
		return id;
	}

	
	
	public void setId(String id) {
		this.id = id;
	}

	public AccordingManager getAccordingManager() {
		return accordingManager;
	}

	public void setAccordingManager(AccordingManager accordingManager) {
		this.accordingManager = accordingManager;
	}

	@Override
	public Inventor getModel() {
		// TODO Auto-generated method stub
		return inventor;
	}

	@Override
	public String list() throws Exception {
			List<Inventor>	inventorList= accordingManager.findInventorOrdById("cid", cid);
			Struts2Utils.getRequest().setAttribute("inventorList", inventorList);
		return SUCCESS;
	}

	@Override
	public String input() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String save() throws Exception {
		Map<String,Itemlist> pp = new HashMap<String,Itemlist>();
		for (String itemId : itemArray) {
			Itemlist itemlist0 = accordingManager.getItemlistById(itemId);
			String code = itemlist0.getCheckCode();
			pp.put(code, itemlist0);
		}
		if(org.apache.commons.lang.StringUtils.isNotEmpty(cid)){
			List<Inventor> Inventors = accordingManager.findInventorOrdById(cid, cid);
			for (int i = 0; i < Inventors.size(); i++) {
				Inventor inventor = Inventors.get(i);
				String codeed = inventor.getCheckCode();
				if(!pp.containsKey(codeed)){
					accordingManager.deleteInventor(cid,codeed);
				}else{
					pp.remove(codeed);
				}
			}
		}
		for(Entry<String, Itemlist> entry: pp.entrySet()) {  
			Inventor inventor=new Inventor();
			Itemlist itemlist = entry.getValue();
			inventor.setCid(this.cid);
			inventor.setBranchCode(itemlist.getBranchCode());
			inventor.setBranchName(itemlist.getBranchName());
			inventor.setCheckCode(itemlist.getCheckCode());
			inventor.setCheckName(itemlist.getCheckName());
			inventor.setUnitPrice(itemlist.getUnitPrice());
			inventor.setUnits(itemlist.getUnits());
			accordingManager.saveInventor(inventor);
		}
		return "inventotSave";
	}

	@Override
	public String delete() throws Exception {
	
		return null;
	}
	
	public String update() throws Exception {
		for(int i = 1;i < acc.size();i++){
			Acc ac = acc.get(i);
			Inventor inventor = accordingManager.findInventorByValue("id",ac.getValue());
			inventor.setMeasure(Double.valueOf(ac.getLabel()));
			accordingManager.saveInventor(inventor);
		}
		return "load";
	}
	
	public String view() throws Exception {
		Document document = accordingManager.findDocumentByValue("id", cid);
		List<According> accordings = accordingManager.findAccordingList(document);
		List<Inventor> inventors = accordingManager.findInventorOrdById("cid", cid);
		double sum = 0;
		for (int i = 0; i < inventors.size(); i++) {
			Inventor inventor = inventors.get(i);
			sum += inventor.getMeasure()*inventor.getUnitPrice();
		}
		String chinaNumber = ConvertNumber.numberToChinese(sum);
		Struts2Utils.getRequest().setAttribute("sum", sum);
		Struts2Utils.getRequest().setAttribute("chinaNumber", chinaNumber);
		Struts2Utils.getRequest().setAttribute("document", document);
		Struts2Utils.getRequest().setAttribute("accordings", accordings);
		Struts2Utils.getRequest().setAttribute("inventors", inventors);
		return "view";
	}
	
	public String down() throws Exception {
		Document document = accordingManager.findDocumentByValue("id", cid);
		List<According> accordings = accordingManager.findAccordingList(document);
		List<Inventor> inventors = accordingManager.findInventorOrdById("cid", cid);
		double sum = 0;
		for (int i = 0; i < inventors.size(); i++) {
			Inventor inventor = inventors.get(i);
			sum += inventor.getMeasure()*inventor.getUnitPrice();
		}
		String chinaNumber = ConvertNumber.numberToChinese(sum);
		Struts2Utils.getRequest().setAttribute("sum", sum);
		Struts2Utils.getRequest().setAttribute("chinaNumber", chinaNumber);
		Struts2Utils.getRequest().setAttribute("document", document);
		Struts2Utils.getRequest().setAttribute("accordings", accordings);
		Struts2Utils.getRequest().setAttribute("inventors", inventors);
		return "down";
	}
	@Override
	protected void prepareModel() throws Exception {
		// TODO Auto-generated method stub
		if(StringUtils.isNotEmpty(id)){
			inventor=accordingManager.findInventorByValue("id", id);
		}
			inventor=new Inventor();
		
	}

}
